<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveSupport::Callbacks</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveSupport::Callbacks 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/activesupport/lib/active_support/callbacks_rb.html">activesupport/lib/active_support/callbacks.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p><a href="Callbacks.html">Callbacks</a> are code hooks that are run at key
points in an object's lifecycle. The typical use case is to have a base
class define a set of callbacks relevant to the other functionality it
supplies, so that subclasses can install callbacks that enhance or modify
the base functionality without needing to override or redefine methods of
the base class.</p>

<p>Mixing in this module allows you to define the events in the object’s
lifecycle that will support callbacks (via
<code>ClassMethods.define_callbacks</code>), set the instance methods,
procs, or callback objects to be called (via
<code>ClassMethods.set_callback</code>), and run the installed callbacks at
the appropriate times (via <code>run_callbacks</code>).</p>

<p>Three kinds of callbacks are supported: before callbacks, run before a
certain event; after callbacks, run after the event; and around callbacks,
blocks that surround the event, triggering it when they yield. Callback
code can be contained in instance methods, procs or lambdas, or callback
objects that respond to certain predetermined methods. See
<code>ClassMethods.set_callback</code> for details.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Record</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Callbacks</span>
  <span class="ruby-identifier">define_callbacks</span> :<span class="ruby-identifier">save</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">save</span>
    <span class="ruby-identifier">run_callbacks</span> :<span class="ruby-identifier">save</span> <span class="ruby-keyword">do</span>
      <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;- save&quot;</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">PersonRecord</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Record</span>
  <span class="ruby-identifier">set_callback</span> :<span class="ruby-identifier">save</span>, :<span class="ruby-identifier">before</span>, :<span class="ruby-identifier">saving_message</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">saving_message</span>
    <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;saving...&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">set_callback</span> :<span class="ruby-identifier">save</span>, :<span class="ruby-identifier">after</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">object</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;saved&quot;</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span> = <span class="ruby-constant">PersonRecord</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">save</span>
</pre>

<p>Output:</p>

<pre>saving...
- save
saved</pre>

    </div>
  


  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">MODULE</span>
          <a href="Callbacks/ClassMethods.html">ActiveSupport::Callbacks::ClassMethods</a>
        </li>
      
    </ul>
  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>R</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Callbacks.html#method-i-run_callbacks">run_callbacks</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    
      <!-- Section constants -->
      <div class="sectiontitle">Constants</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class="attr-name">CALLBACK_FILTER_TYPES</td>
            <td>=</td>
            <td class="attr-value">[:before, :after, :around]</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
      </table>
    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-run_callbacks">
            
              <b>run_callbacks</b>(kind, &amp;block)
            
            <a href="Callbacks.html#method-i-run_callbacks" name="method-i-run_callbacks" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Runs the callbacks for the given event.</p>

<p>Calls the before and around callbacks in the order they were set, yields
the block (if given one), and then runs the after callbacks in reverse
order.</p>

<p>If the callback chain was halted, returns <code>false</code>. Otherwise
returns the result of the block, or <code>true</code> if no block is given.</p>

<pre>run_callbacks :save do
  save
end</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-run_callbacks_source')" id="l_method-i-run_callbacks_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/6d8b39095ca4274bdec8c3a2307efae8130c306c/activesupport/lib/active_support/callbacks.rb#L78" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-run_callbacks_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/callbacks.rb, line 78</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">run_callbacks</span>(<span class="ruby-identifier">kind</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">runner_name</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">__define_callbacks</span>(<span class="ruby-identifier">kind</span>, <span class="ruby-keyword">self</span>)
  <span class="ruby-identifier">send</span>(<span class="ruby-identifier">runner_name</span>, &amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    